package com.ctyon.watchmanager.util;

import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * CreateDate：19-8-8 on 下午2:01
 * Describe:
 * Coder: lee
 */
public class BytesUtil {

    public static byte[] packData(String path) {
        FileInputStream fis = null;
        List<Byte> data = null;
        try {
            fis = new FileInputStream(path);
            int len = fis.available();
            data = new ArrayList<>();
            for (int a = 0; a < len; a++) {
                int res = fis.read();
                switch (res & 0XFF) {
                    case 0X7D:
                        data.add((byte) 0X7D);
                        data.add((byte) 0X01);
                        break;
                    case 0X5B:
                        data.add((byte) 0X7D);
                        data.add((byte) 0X02);
                        break;
                    case 0X5D:
                        data.add((byte) 0X7D);
                        data.add((byte) 0X03);
                        break;
                    case 0X2C:
                        data.add((byte) 0X7D);
                        data.add((byte) 0X04);
                        break;
                    case 0X2A:
                        data.add((byte) 0X7D);
                        data.add((byte) 0X05);
                        break;
                    default:
                        data.add((byte) res);
                        break;
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        byte[] result = new byte[data.size()];
        for (int x = 0; x < data.size(); x++) {
            result[x] = data.get(x);
        }
        Log.d("BytesUtil", "packData : " + result.length);
        return result;
    }

    public static String unpackData(int type, byte[] byteData) {
        byte DATA_KEYWORD1 = (byte) 0X7D;
        byte DATA_KEYWORD2 = (byte) 0X5B;
        byte DATA_KEYWORD3 = (byte) 0X5D;
        byte DATA_KEYWORD4 = (byte) 0X2C;
        byte DATA_KEYWORD5 = (byte) 0X2A;

        String filepath = "";
        FileOutputStream fos = null;
        if (type == 0){
            filepath = getAmrFilePath();
        } else if (type == 1){
            filepath = getImgFilePath();
        }

        int i = 0;
        int len = byteData.length;
        try {
            File file = new File(filepath);
            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();
            fos = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            while (i < len) {
                if (byteData[i] == (byte) 0X7D) {
                    if (i + 1 >= len) {
                        fos.write(byteData[i]);
                        break;
                    }
                    switch (byteData[i + 1]) {
                        case (byte) 0x01:
                            fos.write(DATA_KEYWORD1);
                            i = i + 2;
                            break;
                        case (byte) 0x02:
                            fos.write(DATA_KEYWORD2);
                            i = i + 2;
                            break;
                        case (byte) 0x03:
                            fos.write(DATA_KEYWORD3);
                            i = i + 2;
                            break;
                        case (byte) 0x04:
                            fos.write(DATA_KEYWORD4);
                            i = i + 2;
                            break;
                        case (byte) 0x05:
                            fos.write(DATA_KEYWORD5);
                            i = i + 2;
                            break;
                        default:
                            fos.write(byteData[i]);
                            i = i + 1;
                            break;
                    }
                } else {
                    fos.write(byteData[i]);
                    i = i + 1;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return filepath;
    }

    // 以当前时间作为文件名
    private static String getCurrentDate() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HHmmss");
        Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
        String str = formatter.format(curDate);
        return str;
    }

    public static String getAmrFilePath() {
        String fileName = getCurrentDate();
        String fileFolder = Environment.getExternalStorageDirectory().getPath() + "/watchManager";
        File file = new File(fileFolder);
        if (!file.exists()) {
            file.mkdir();
        }
        return fileFolder + "/" + fileName + ".amr";
    }

    public static String getImgFilePath() {
        String fileName = getCurrentDate();
        String fileFolder = Environment.getExternalStorageDirectory().getPath() + "/watchManager";
        File file = new File(fileFolder);
        if (!file.exists()) {
            file.mkdir();
        }
        return fileFolder + "/" + fileName + ".png";
    }

}
